﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApiBetaSeries.Factories;
using System.Xml;

namespace ApiBetaSeries.Services
{
    public class EpisodeServices : ApiBetaSeries.Services.IEpisodeServices
    {
        #region singleton
        private static EpisodeServices m_episodeServices = null;
        private EpisodeServices() { }
        public static EpisodeServices GetInstance()
        {
            if (m_episodeServices == null)
            {
                m_episodeServices = new EpisodeServices();
            }
            return m_episodeServices;
        }
        #endregion

        /// <summary>
        /// Mark the episode as watched on betaseries
        /// </summary>
        /// <param name="token">token of the member</param>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="numEpisode">episode number</param>
        /// <param name="note">note of the episode (default : none)</param>
        [ApiBetaSeries.Version.ApiMethod(false, "members/watched")]
        public void SetEpisodeWatched(string token, string urlSerie, int season, int numEpisode, int note = -1)
        {
            IEpisode episode = EpisodeFactory.CreateEpisode();
            episode.UrlShow = urlSerie;
            episode.Season = season;
            episode.Number = numEpisode;
            episode.SetWatched(token, note);
        }
        /// <summary>
        /// Mark the episode as watched on betaseries
        /// </summary>
        /// <param name="user">member who have watched the episode</param>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="numEpisode">episode number</param>
        /// <param name="note">note of the episode (default : none)</param>
        [ApiBetaSeries.Version.ApiMethod(false, "members/watched")]
        public void SetEpisodeWatched(IUser user, string urlSerie, int season, int numEpisode, int note = -1)
        {
            IEpisode episode = EpisodeFactory.CreateEpisode();
            episode.UrlShow = urlSerie;
            episode.Season = season;
            episode.Number = numEpisode;
            episode.SetWatched(user, note);
        }

        /// <summary>
        /// Mark the episode as downloaded on betaseries
        /// </summary>
        /// <param name="token">token of the member</param>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="numEpisode">episode number</param>
        /// <returns>if the episode is downloaded</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "members/downloaded")]
        public bool SetEpisodeDownloaded(string token, string urlSerie, int season, int numEpisode)
        {
            IEpisode episode = EpisodeFactory.CreateEpisode();
            episode.UrlShow = urlSerie;
            episode.Season = season;
            episode.Number = numEpisode;
            return episode.SetDownloaded(token);
        }
        /// <summary>
        /// Mark the episode as downloaded on betaseries
        /// </summary>
        /// <param name="user">the member</param>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="numEpisode">episode number</param>
        /// <returns>if the episode is downloaded</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "members/downloaded")]
        public bool SetEpisodeDownloaded(IUser user, string urlSerie, int season, int numEpisode)
        {
            IEpisode episode = EpisodeFactory.CreateEpisode();
            episode.UrlShow = urlSerie;
            episode.Season = season;
            episode.Number = numEpisode;
            return episode.SetDownloaded(user);
        }

        /// <summary>
        /// get all episodes of the member
        /// </summary>
        /// <param name="token">token of the member</param>
        /// <param name="type">VO/VOVF/All</param>
        /// <param name="next">Only the next episodes to watch</param>
        /// <returns>list of episodes</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "members/episodes")]
        public List<IEpisode> GetEpisodes(string token, TypeEpisode type = TypeEpisode.All, bool next = false)
        {
            IUser user = UserFactory.CreateUser();
            user.Token = token;
            return GetEpisodes(user, type, next);
        }

        /// <summary>
        /// get all episodes of the member
        /// </summary>
        /// <param name="user">the member</param>
        /// <param name="type">VO/VOVF/All</param>
        /// <param name="next">Only the next episodes to watch</param>
        /// <returns>list of episodes</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "members/episodes")]
        public List<IEpisode> GetEpisodes(IUser user, TypeEpisode type = TypeEpisode.All, bool next = false)
        {
            return user.GetEpisodes(type, next);
        }

        /// <summary>
        /// Get a specific episode of a show
        /// </summary>
        /// <param name="url">url of the show</param>
        /// <param name="seasonNumber">season of the episode</param>
        /// <param name="episodeNumber">episode number</param>
        /// <param name="token">Token of the user</param>
        /// <returns>the episode</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "shows/episodes")]
        public IEpisode GetEpisode(string url, int seasonNumber, int episodeNumber, string token = null)
        {
            IShow show = ShowFactory.CreateShow();
            show.Url = url;
            return show.GetEpisode(seasonNumber, episodeNumber, token);
        }

        /// <summary>
        /// Get a specific episode of a show
        /// </summary>
        /// <param name="url">url of the show</param>
        /// <param name="seasonNumber">season of the episode</param>
        /// <param name="episodeNumber">episode number</param>
        /// <param name="user">the member</param>
        /// <returns>the episode</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "shows/episodes")]
        public IEpisode GetEpisode(string url, int seasonNumber, int episodeNumber, IUser user)
        {
            return GetEpisode(url, seasonNumber, episodeNumber, user.Token);
        }


        /// <summary>
        /// mark an episode
        /// </summary>
        /// <param name="token">token of the member</param>
        /// <param name="urlShow">url of the show</param>
        /// <param name="season">season number</param>
        /// <param name="episode">episode number</param>
        /// <param name="mark">mark of this episode</param>
        [ApiBetaSeries.Version.ApiMethod(false, "members/note")]
        public void SetMark(string token, string urlShow, int season, int episode, int mark)
        {
            IEpisode ep = EpisodeFactory.CreateEpisode();
            ep.UrlShow = urlShow;
            ep.Season = season;
            ep.Number = episode;
            ep.SetMark(token, mark);
        }
        /// <summary>
        /// mark an episode
        /// </summary>
        /// <param name="user">the member</param>
        /// <param name="urlShow">url of the show</param>
        /// <param name="season">season number</param>
        /// <param name="episode">episode number</param>
        /// <param name="mark">mark of this episode</param>
        [ApiBetaSeries.Version.ApiMethod(false, "members/note")]
        public void SetMark(IUser user, string urlShow, int season, int episode, int mark)
        {
            SetMark(user.Token, urlShow, season, episode, mark);
        }


    }
}
